#include "recboxitem.h"
#include "ui_recboxitem.h"
#include<QPropertyAnimation>

RecBoxItem::RecBoxItem(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::RecBoxItem)
{
    ui->setupUi(this);

    //安装事件拦截器
    ui->musicImageBox->installEventFilter(this);
}

RecBoxItem::~RecBoxItem()
{
    delete ui;
}

void RecBoxItem::setText(QString text)
{
    ui->recBoxItemText->setText(text);
}

void RecBoxItem::setImage(QString imagePath)
{
    QString imageStyle = "border-image:url("+imagePath+");";
    ui->recMusicImage->setStyleSheet(imageStyle);
}

//拦截鼠标进入和离开时的事件处理
bool RecBoxItem::eventFilter(QObject *watch, QEvent *event)
{
    if(watch == ui->musicImageBox){
        int imageWidth = ui->musicImageBox->width();
        int imageHeight = ui->musicImageBox->height();

        if(event->type() == QEvent::Enter){
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox,"geometry");
            animation->setDuration(200);
            animation->setStartValue(QRect(6,7,imageWidth,imageHeight));
            animation->setEndValue(QRect(6,0,imageWidth,imageHeight));
            animation->start();

            //动画结束会发送finished信号，拦截该信号并销毁animation对象
            connect(animation, &QPropertyAnimation::finished, this, [=](){
                delete animation;
            });
            return true;
        }else if(event->type() == QEvent::Leave){
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox,"geometry");
            animation->setDuration(200);
            animation->setStartValue(QRect(6,0,imageWidth,imageHeight));
            animation->setEndValue(QRect(6,7,imageWidth,imageHeight));
            animation->start();

            //动画结束会发送finished信号，拦截该信号并销毁animation对象
            connect(animation, &QPropertyAnimation::finished, this, [=](){
                delete animation;
            });
            return true;
        }

    }
    return QObject::eventFilter(watch, event);
}
